<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

/**
 +----------------------------------------------------------
 * 压缩PHP文件内容和简单加密
 +----------------------------------------------------------
 * @param string $filename 文件名
 * @param boolean $strip 是否去除代码空白和注释
 +----------------------------------------------------------
 * @return false|integer 返回加密文件的字节大小
 +----------------------------------------------------------
 */
function encode_file_contents($filename,$strip=true)
{
    $type = strtolower(substr(strrchr($filename, '.'),1));
    if('php'==$type && is_file($filename) && is_writeable($filename)) {
        // 如果是PHP文件 并且可写 则进行压缩
    	$contents  =  file_get_contents($filename);
        // 判断文件是否已经被加密
        $pos = strpos($contents,'/*Protected by ThinkPHP Cryptation*/');
        if( false === $pos  || $pos>100 ) {
            if($strip) {
                // 去除PHP文件注释和空白，减少文件大小
                $contents  =  php_strip_whitespace($filename);
            }
            // 去除PHP头部和尾部标识
            $headerPos   =  strpos($contents,'<?php');
            $footerPos    =  strrpos($contents,'?>');
            $contents  =  substr($contents,$headerPos+5,$footerPos-$headerPos);
            // 对文件内容进行加密存储
            $encode   =  base64_encode(gzdeflate($contents,1));
            $encode   = '<?php'." /*Protected by ThinkPHP Cryptation*/\n\$cryptCode='".$encode."';eval(gzinflate(base64_decode(\$cryptCode)));\n /*Reverse engineering is illegal and strictly prohibited - (C) ThinkPHP Cryptation 2006~2008*/\n?>";
            // 重新写入加密内容
            return file_put_contents($filename,$encode);
        }
    }
    return false;
}
/**
 +----------------------------------------------------------
 * 去掉PHP文件的注释和空白
 +----------------------------------------------------------
 * @param string $filename 文件名
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
function strip_file_contents($filename)
{
    $type = strtolower(substr(strrchr($filename, '.'),1));
    if('php'==$type && is_file($filename) && is_writeable($filename)) {
        // 去除PHP文件注释和空白，减少文件大小
        $contents  =  php_strip_whitespace($filename);
        return file_put_contents($filename,$contents);
    }
}

/**
 +----------------------------------------------------------
 * 把目录下面的PHP文件去掉注释和空白
 +----------------------------------------------------------
 * @param string $path 路径
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
function strip_dir($path)
{
        if(substr($path, -1) != "/")    $path .= "/";
        $dir=glob($path."*");
        foreach($dir as $key=>$val) {
            if(is_dir($val)) {
                strip_dir($val);
            } else{
                strip_file_contents($val);
            }
        }
}

/**
 +----------------------------------------------------------
 * 压缩文件夹下面的PHP文件
 +----------------------------------------------------------
 * @param string $path 路径
 +----------------------------------------------------------
 * @return void
 +----------------------------------------------------------
 */
function encode_dir($path) {
    if(substr($path, -1) != "/")    $path .= "/";
    $dir=glob($path."*");
    foreach($dir as $key=>$val) {
        if(is_dir($val)) {
            encode_dir($val);
        } else{
            encode_file_contents($val);
        }
    }
}
// 创建项目目录结构
function buildAppDir() {
    // 没有创建项目目录的话自动创建
    if(!is_dir(APP_PATH)){
        mkdir(APP_PATH);
    }
    if(is_writeable(APP_PATH)) {
        // 创建项目应用目录
        mkdirs(array(
            LIB_PATH,  // 项目应用目录
            CONFIG_PATH,   // 项目配置目录
            COMMON_PATH, // 项目公共目录
            LANG_PATH,  // 项目语言包目录
            RUNTIME_PATH, // 项目运行目录
            // MVC 目录
            LIB_PATH.'Model/',
            LIB_PATH.'Action/',
            LIB_PATH.'View/',
            // 默认模板主题目录
            LIB_PATH.'View/default/'
            ));
        // 目录安全写入
        if(!defined('BUILD_DIR_SECURE')) define('BUILD_DIR_SECURE',false);
        if(BUILD_DIR_SECURE) {
            if(!defined('DIR_SECURE_FILENAME')) define('DIR_SECURE_FILENAME','index.html');
            if(!defined('DIR_SECURE_CONTENT')) define('DIR_SECURE_CONTENT',' ');
            // 自动写入目录安全文件
            $content		=	DIR_SECURE_CONTENT;
            $a = explode(',', DIR_SECURE_FILENAME);
            foreach ($a as $filename){
                file_put_contents(LIB_PATH.$filename,$content);
                file_put_contents(LIB_PATH.'Action/'.$filename,$content);
                file_put_contents(LIB_PATH.'Model/'.$filename,$content);
                file_put_contents(LIB_PATH.'View/'.$filename,$content);
                file_put_contents(LIB_PATH.'View/default/'.$filename,$content);
                file_put_contents(RUNTIME_PATH.$filename,$content);
                file_put_contents(LANG_PATH.$filename,$content);
                file_put_contents(COMMON_PATH.$filename,$content);
                file_put_contents(CONFIG_PATH.$filename,$content);
            }
        }
        // 写入测试Action
        if(!file_exists(LIB_PATH.'Action/IndexAction.class.php')) {
            $content	 =
    '<?php
    // 本类由系统自动生成，仅供测试用途
    class IndexAction extends Action{
    public function index(){
        header("Content-Type:text/html; charset=utf-8");
        echo "<div style=\'font-weight:normal;color:blue;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;font-size:14px;font-family:Tahoma\'>^_^ Hello,欢迎使用<span style=\'font-weight:bold;color:red\'>ThinkPHP</span></div>";
    }
    }
    ?>';
            file_put_contents(LIB_PATH.'Action/IndexAction.class.php',$content);
        }
    }else{
        header("Content-Type:text/html; charset=utf-8");
        exit('<div style=\'font-weight:bold;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>项目目录不可写，目录无法自动生成！<BR>请使用项目生成器或者手动生成项目目录~</div>');
    }
}

// 检查缓存目录(Runtime) 如果不存在则自动创建
function checkRuntime() {
    if(!is_writeable(RUNTIME_PATH)) {
        header("Content-Type:text/html; charset=utf-8");
        exit('<div style=\'font-weight:bold;float:left;width:345px;text-align:center;border:1px solid silver;background:#E8EFFF;padding:8px;color:red;font-size:14px;font-family:Tahoma\'>目录 [ '.RUNTIME_PATH.' ] 不可写！</div>');
    }
    mkdirs(array(
        TMPL_CACHE_PATH,    // 模板缓存目录
        LOG_PATH,    // 日志目录
        TEMP_PATH,  // 数据缓存目录
        DATA_PATH   // 数据文件目录
        ));
    return true;
}
function mkdirs($dirs,$mode=0777) {
    if(is_string($dirs)) {
        $dirs  = explode(',',$dirs);
    }
    foreach ($dirs as $dir){
        if(!is_dir($dir))  mkdir($dir,$mode);
    }
}
?>